/***************************************************************************
 *   Copyright (C) 2006 by Sergey Melderis   *
 *   sergey.melderis@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "tooltip.h"
#include <qtimer.h>
#include <qrect.h>
#include <qfont.h>
#include <qsize.h>
#include <qpainter.h>
#include <qbitmap.h>
#include <qpixmap.h>
#include <qpalette.h>
#include <qregexp.h>
#include <qimage.h>
#include <qapplication.h>
Tooltip::Tooltip(QWidget *parent, const char *name)
 : QWidget(parent, name, WType_TopLevel | WNoAutoErase | WStyle_Customize | WX11BypassWM | WStyle_StaysOnTop)
{
    setFocusPolicy( NoFocus );
    setBackgroundMode( NoBackground );

   m_timer = new QTimer(this);
   hide();

   connect( m_timer, SIGNAL(timeout()), this,  SLOT(hide()) );
}


Tooltip::~Tooltip()
{
}

// The following methods are based on Amarok and Kicker sources.

QRect Tooltip::determineMetrics(const QPoint &point)
{
    m_font = QFont("Sans-Serif", 12, QFont::Bold);
    QWidget *myParent = static_cast<QWidget*>(parent());
    int maxWidth = myParent->width() - 20;

     // If we don't do that, the boundingRect() might not be suitable for drawText() (Qt issue N67674)
    m_text.replace( QRegExp(" +\n"), "\n" );
    // remove consecutive line breaks
    m_text.replace( QRegExp("\n+"), "\n" );


    QFontMetrics fm = QFontMetrics(m_font);
    QRect rect = fm.boundingRect(0, 0, maxWidth, 100, Qt::AlignAuto,  m_text, -1);
//    qDebug("determineMetrics():  width: %i , height: %i  ", rect.width(), rect.height());
    int t_height = rect.height();
    if(rect.width() >= maxWidth - 20)
    {
        m_text.replace(" - ", " - \n");
        rect = fm.boundingRect(0, 0, maxWidth, 100, Qt::AlignAuto | WordBreak,  m_text, -1);
    }
    rect.setHeight(rect.height() + 10);
    rect.setWidth(rect.width() + 20);
    rect.moveLeft((myParent->width() - rect.width()) / 2 + myParent->x());
    rect.moveTop(point.y() - 40);
    return rect;
}

void Tooltip::render(QRect &_rect)
{
    QSize size = _rect.size();
      // From Amarok sources
    const uint M = fontMetrics().width( 'x' );
    const uint xround = (M * 200) / size.width();
    const uint yround = (M * 200) / size.height();

    QPoint point;
    QRect rect(point, size);

    {   /// apply the mask
        static QBitmap mask;

        mask.resize( size );
        mask.fill( Qt::black );
        QPainter p( &mask );
        p.setBrush( Qt::white );
        p.drawRoundRect( rect, xround, yround );
        setMask( mask );
    }

    m_pixmap.resize(size);
    QPainter bufferPainter(&m_pixmap);

    bufferPainter.setPen(Qt::black);
    bufferPainter.setBrush(QColor(0, 102, 204));
    bufferPainter.drawRoundRect(rect, xround, yround);

    bufferPainter.setPen(Qt::white);
    bufferPainter.setFont(m_font);
    bufferPainter.setBrush(Qt::white);
    bufferPainter.drawText(rect, Qt::AlignCenter | WordBreak,m_text);


    bufferPainter.end();
}

void Tooltip::display(const QString &text, const QPoint &point) {
    m_text = text;
    QRect geometry = determineMetrics(point);

    render(geometry);
    setGeometry(geometry);
    show();
    m_timer->start(TIMER_DURATION);

}

void Tooltip::paintEvent(QPaintEvent *e)
{
    QPainter p(this);
    p.drawPixmap(e->rect(), m_pixmap);
}



#include "tooltip.moc"
